
 /**
 * ConfigUtils.java.java.
 * User: BrianChen Date: 2012-3-13 上午05:27:19
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.util;

import java.io.File;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-3-13 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-3-13 上午05:27:19
 * @version 1.0
 * @see
 * 
 */

public class GlobalConfig {
	private static final Logger logger = LoggerFactory.getLogger(GlobalConfig.class);
	
	private static String PROJECT_HOME = "project.home";
	private static String CONTEXT_PATH = "project.context.path";
	private static String CONFIG_PATH = "project.config.path";
	private static String CONFIG_FILE_NAME = "global-config.xml";
	
	private static String KEY_PAGE_SIZE = "pager.pageSize";
	
	private static String KEY_FILEUPLOAD_REPOSITORY_PATH = "fileUpload.repositoryPath";
	private static String KEY_FILEUPLOAD_FILE_ALLOWED_EXTENSIONS = "fileUpload.allowedExtensions";
	private static String KEY_FILEUPLOAD_FILE_DENIED_EXTENSIONS = "fileUpload.deniedExtensions";
	private static String KEY_FILEUPLOAD_FILE_ALLOWED_MAX_SIZE = "fileUpload.maxAllowedFileSize";
	
	private static XMLConfiguration GLOBAL_CONFIG = null;
	static{
		String configPath = getConfigPath();
		String globalXMLConfigPath = concatenateFilepathAndName(configPath, CONFIG_FILE_NAME);
		try {
			GLOBAL_CONFIG = new XMLConfiguration();
			GLOBAL_CONFIG.load(new File(globalXMLConfigPath));
			logger.info("@@@@@loading global-config.xml...");
		} catch (ConfigurationException ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * 获取 分页大小
	 * @return
	 */
	public static int getPageSize(){
		return GLOBAL_CONFIG.getInt(KEY_PAGE_SIZE);
	}
	
	/**
	 * 获取 允许最大文件
	 * @return
	 */
	public static long getAllowedMaxFileSize(){
		return GLOBAL_CONFIG.getLong(KEY_FILEUPLOAD_FILE_ALLOWED_MAX_SIZE);
	}
	/**
	 * 获取 资源库路径
	 */
	public static String getRepositoryPath(){
		return GLOBAL_CONFIG.getString(KEY_FILEUPLOAD_REPOSITORY_PATH);
	}
	/**
	 * 获取 允许的文件格式
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<String> getAllowedFileExts(){
		return GLOBAL_CONFIG.getList(KEY_FILEUPLOAD_FILE_ALLOWED_EXTENSIONS, null);
	}
	/**
	 * 获取 拒绝文件格式
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<String> getDeniedFileExts(){
		return GLOBAL_CONFIG.getList(KEY_FILEUPLOAD_FILE_DENIED_EXTENSIONS, null);
	}
	
	
	/**
	 * 获取项目的起始目录，用绝对路径表示
	 */
	private static String getHomePath(){
		String homePath = System.getProperty(PROJECT_HOME);
		logger.info("@@@@@homePath from jvm(-D..)=" + homePath);
		if(StringUtils.isBlank(homePath)){
			homePath = System.getProperty("user.dir");
			logger.info("@@@@@homePath from jvm(user.dir)=" + homePath);
		}
		
		homePath = new File(homePath).getAbsolutePath();
		logger.info("@@@@@homePath=" + homePath);
		return homePath;
	}
	
	/**
	 * 获取WEB上下文路径
	 */
	private static String getContextPath(){
		String contextPath = System.getProperty(CONTEXT_PATH);
		if(StringUtils.isBlank(contextPath)){
			String homePath = getHomePath();
			contextPath = concatenateFilepathAndName(homePath, "WebRoot");
		}
		logger.info("@@@@@contextPath=" + contextPath);
		return contextPath;
	}
	
	/**
	 * 获取配置文件的路劲，用绝对路径表示
	 */
	private static String getConfigPath(){
		String configPath = System.getProperty(CONFIG_PATH);
		if(StringUtils.isBlank(configPath)){
			String contextPath = getContextPath();
			configPath = concatenateFilepathAndName(contextPath, "WEB-INF");
			configPath = concatenateFilepathAndName(configPath, "classes");
		}
		logger.info("@@@@@configPath=" + configPath);
		return configPath;
	}
	
	/**
	 * 连接文件所在目录和文件名称
	 * @param dirPath
	 * @param fileName
	 * @return 文件路径
	 */
	private static String concatenateFilepathAndName(String dirPath, String fileName){
    	if (dirPath.endsWith(File.separator)) {
    		dirPath = dirPath + fileName;
		} else  {
			dirPath = dirPath + File.separator + fileName;
		}
    	return dirPath;
    }
	
	
}

